include $(TOPDIR)/rules.mk

PKG_NAME:=firmware
PKG_VERSION:=1.0
PKG_RELEASE:=1

include $(INCLUDE_DIR)/package.mk

define Package/firmware/Default
SECTION:=leenchan
CATEGORY:=leenchan
TITLE:=Firmware
PKGARCH:=all
endef

define Package/firmware-16m
$(call Package/firmware/Default)
VARIANT:=16m
TITLE=16M
endef

define Package/firmware-8m
$(call Package/firmware/Default)
VARIANT:=8m
TITLE=8M
endef

define Package/firmware-16m/description
Firmware for 16M
endef

define Package/firmware-8m/description
Firmware for 8M
endef

define Build/Compile
endef

define Package/firmware-16m/postinst
#!/bin/sh
( . /etc/uci-defaults/luci-firmware ) && rm -f /etc/uci-defaults/luci-firmware
exit 0
endef

Package/firmware-8m/postinst = $(Package/firmware-16m/postinst)

define Package/firmware-16m/prerm
#!/bin/sh
/etc/init.d/firmware disable >/dev/null 2>&1
exit 0
endef

Package/firmware-8m/prerm = $(Package/firmware-16m/prerm)

define Package/firmware-16m/postrm
#!/bin/sh
uci -q batch <<-EOF >/dev/null
	delete ucitrack.@firmware[-1]
	commit ucitrack
EOF
rm -f /tmp/luci-indexcache
rm -rf /tmp/luci-modulecache
exit 0
endef

Package/firmware-8m/postrm = $(Package/firmware-16m/postrm)

define Package/firmware-16m/install
	$(CP) ./root/* $(1)
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/firmware.init $(1)/etc/init.d/firmware
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_CONF) ./files/firmware.config.16m $(1)/etc/config/firmware
	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_BIN) ./files/luci-firmware.uci $(1)/etc/uci-defaults/luci-firmware
	$(INSTALL_DIR) $(1)/etc
	echo $(shell LC_ALL=C date "+%Y%m%d%H%M") > $(1)/etc/firmware
endef

define Package/firmware-8m/install
	$(CP) ./root/* $(1)
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/firmware.init $(1)/etc/init.d/firmware
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_CONF) ./files/firmware.config.8m $(1)/etc/config/firmware
	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_BIN) ./files/luci-firmware.uci $(1)/etc/uci-defaults/luci-firmware
	$(INSTALL_DIR) $(1)/etc
	echo $(shell LC_ALL=C date "+%Y%m%d%H%M") > $(1)/etc/firmware
endef

$(eval $(call BuildPackage,firmware-16m))
$(eval $(call BuildPackage,firmware-8m))
